package com.me.waterphase;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class WaterAnimation {

	private static final int FRAME_COLS = 6;
	private static final int FRAME_ROWS = 5;
	
	private Animation walkAnimation;
	private Texture walkSheet;
	private TextureRegion[] walkFrames;
	private TextureRegion currentFrame;
	private float width;
	private float height;
	
	/*
	 * Getters and setters.
	 */
	public Animation getWalkingAnimation(){
		return walkAnimation;
	}
	
	public Texture getWalkingSheet(){
		return walkSheet;
	}
	
	public TextureRegion[] getWalkingFrames(){
		return walkFrames;
	}
	
	public TextureRegion getCurrentFrame(){
		return currentFrame;
	}
		
	public void setWalkingAnimation(Animation _walkAnimation){
		walkAnimation = _walkAnimation;
	}
	
	public void setWalkingSheet(Texture _walkSheet){
		walkSheet = _walkSheet;
	}
	
	public void setWalkingFrames(TextureRegion[] _walkFrames){
		walkFrames = _walkFrames;
	}
	
	public void setCurrentFrame(TextureRegion _currentFrame){
		currentFrame = _currentFrame;
	}
	
	/*
	 * Constructors.
	 */
	public WaterAnimation(String _animationName, float _width, float _height){
		
		//Set the proportion.
		width = _width;
		height = _height;
		
		//Get the sheet.
		walkSheet = new Texture(Gdx.files.internal(GraphicManager.MEDIA_ANIM_PATH + _animationName));
		
		TextureRegion[][] tmp = TextureRegion.split(walkSheet, walkSheet.getWidth()/ FRAME_COLS, walkSheet.getHeight()/FRAME_ROWS);
		
		walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
        int index = 0;
        for (int i = 0; i < FRAME_ROWS; i++) {
                for (int j = 0; j < FRAME_COLS; j++) {
                        walkFrames[index++] = tmp[i][j];
                }
        }
        walkAnimation = new Animation(0.025f, walkFrames);
	}
	
	public void render(float _stateTime, float _posX, float _posY){
	     //Get the right frame.
		 currentFrame = walkAnimation.getKeyFrame(_stateTime, true);

		 //Draw on the scene.
		 WaterWorld.getStage().begin();
		 WaterWorld.getStage().draw(currentFrame, _posX, _posY, width, height);                        
		 WaterWorld.getStage().end();
	}
	
	public void dispose()
	{
		walkSheet.dispose();
	}
}
